#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btn_openFile_clicked()
{
    QString dialogCaption = "打开一个文件测试";
    QString dialogDefaultDir = QDir::currentPath();
    QString dialogFilter = "markdown文件(*.md);;word文档(*.doc *.docx);;文本文件(*.txt);;代码源文件(*.c *.cpp);;代码头文件(*.h *.cpp);;代码文件(*.cpp *.c *.h)";
    QString dialogSelectFilted = "代码文件(*.cpp *.c *.h)";

    QString openFileName = QFileDialog::getOpenFileName(this,dialogCaption,dialogDefaultDir,dialogFilter,&dialogSelectFilted);

    if(openFileName.isEmpty())
        return;

    qDebug() << openFileName;

    QFile readFile(openFileName);
    readFile.open(QIODevice::ReadOnly);
#if 1
    ui->textEdit->setText(readFile.readAll());
#else
    ui->textEdit->clear();
    while(!readFile.atEnd())
        ui->textEdit->append(QString(readFile.readLine(0).trimmed()));//读一行直到遇到换行符trimmed(),也会移除 所有行首和行尾的空白字符（ , \t, \n, \r 等）
#endif
    readFile.close();
}


void MainWindow::on_btn_saveFile_clicked()
{
    QString dialogCaption = "保存一个文件测试";
    QString dialogDefaultDir = QDir::currentPath();
    QString dialogSelectFilted = "文本文件(*.txt);;代码源文件(*.c *.cpp);;代码头文件(*.h *.cpp)";

    QString saveFileName = QFileDialog::getSaveFileName(this,dialogCaption,dialogDefaultDir,dialogSelectFilted);

    if(saveFileName.isEmpty())
        return;

    qDebug() << saveFileName;

    QFile saveFile(saveFileName);
    saveFile.open(QIODevice::WriteOnly);

    QString tmp = ui->textEdit->toPlainText();
    QByteArray wdata = tmp.toUtf8();
    saveFile.write(wdata);

    saveFile.close();
}


void MainWindow::on_btn_stream_read_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,"测试流式读取文件");

    if(fileName.isEmpty())
    {
        qDebug() <<"文件名为空";
        return;
    }


    QFile openFile(fileName);
    if(!openFile.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream mystream(&openFile);

    ui->textEdit->setText(mystream.readAll());
}


void MainWindow::on_btn_stream_write_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,"测试流式保存文件");
    if(fileName.isEmpty())
        return;

    QFile openFileNme(fileName);
    if(!openFileNme.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return;
    }

    QTextStream tStream(&openFileNme);
    tStream << ui->textEdit->toPlainText();
}

/*QDataStream适合序列化和反序列化*/
void MainWindow::on_btn_QDataStream_read_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,"测试QDataStream读取文件");
    if(fileName.isEmpty())
        return;

    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly))
        return;

    QDataStream mystream(&file);

    int age;
    QString name,password;

    while(!mystream.atEnd())
    {
        mystream >> name >> password >> age;
        ui->textEdit->append(QString("%1 %2 %3").arg(name).arg(password).arg(age));
    }
}


void MainWindow::on_QDataStream_write_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,"测试QDataStream写入数据");
    if(fileName.isEmpty())
        return;

    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly))
        return;

    QDataStream mystream(&file);

    int age;
    QString name,password;

    name = "悯醨";password = "123456";age = 21;
    mystream << name << password << age;
    name = "wanxao";password = "000000";age = 21;
    mystream << name << password << age;
    name = "minli";password = "159357";age = 21;
    mystream << name << password << age;

}

